﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleWashFactorsRepo
    {
        public const string m_msg_head = "Title WashFactors";

        public IEnumerable<TitleWashFactorsModels> GetTitleWashFactors()
        {
            List<TitleWashFactorsModels> TitleWashFactorss = new List<TitleWashFactorsModels>();
            string SQLCommand = @"select * from TBTitleProcedure WHERE Status <> 'Delete' order by TitleWashFactorsId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleWashFactorsModels TitleWashFactors = new TitleWashFactorsModels();
                        TitleWashFactors.TitleWashFactorsId = (int)reader["TitleWashFactorsId"];
                        TitleWashFactors.TitleWashFactorsType = (string)reader["TitleWashFactorsType"];
                        TitleWashFactors.TitleWashFactorsName = (string)reader["TitleWashFactorsName"];
                        TitleWashFactors.Status = (string)reader["Status"];

                        TitleWashFactorss.Add(TitleWashFactors);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleWashFactorss.ToList();
        }
        public IEnumerable<TitleWashFactorsModels> GetTitleWashFactorsDropDown()
        {
            List<TitleWashFactorsModels> TitleWashFactorss = new List<TitleWashFactorsModels>();
            string SQLCommand = @"SELECT distinct TitleWashFactorsType FROM TBTitleWashFactors WHERE Status <> 'Delete'";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleWashFactorsModels TitleWashFactors = new TitleWashFactorsModels();
                        TitleWashFactors.TitleWashFactorsType = (string)reader["TitleWashFactorsType"];

                        TitleWashFactorss.Add(TitleWashFactors);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleWashFactorss.ToList();
        }
        public TitleWashFactorsModels GetTitleWashFactorsById(int id)
        {
            TitleWashFactorsModels TitleWashFactors = new TitleWashFactorsModels();
            string SQLCommand = @"select * from TBTitleWashFactors where TitleWashFactorsId=@TitleWashFactorsId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleWashFactorsId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleWashFactors.TitleWashFactorsId = (int)reader["TitleWashFactorsId"];
                        TitleWashFactors.TitleWashFactorsType = (string)reader["TitleWashFactorsType"];
                        TitleWashFactors.TitleWashFactorsName = (string)reader["TitleWashFactorsName"];
                        TitleWashFactors.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleWashFactors;
        }

        public bool Create(TitleWashFactorsModels TitleWashFactors)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleWashFactors
                                    values (@TitleWashFactorsType,@TitleWashFactorsName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleWashFactorsType", TitleWashFactors.TitleWashFactorsType);
                    db.AddParameter("@TitleWashFactorsName", TitleWashFactors.TitleWashFactorsName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleWashFactorsModels TitleWashFactors)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleWashFactors 
                                    Set TitleWashFactorsType=@TitleWashFactorsType,
                                        TitleWashFactorsName=@TitleWashFactorsName
                                    where TitleWashFactorsId=@TitleWashFactorsId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleWashFactorsId", TitleWashFactors.TitleWashFactorsId);
                    db.AddParameter("@TitleWashFactorsType", TitleWashFactors.TitleWashFactorsType);
                    db.AddParameter("@TitleWashFactorsName", TitleWashFactors.TitleWashFactorsName);
                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleWashFactorsModels TitleWashFactors)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleWashFactors
                                    Set Status=@Status
                                    where TitleWashFactorsId=@TitleWashFactorsId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleWashFactorsId", TitleWashFactors.TitleWashFactorsId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}